<?php
/**
 * @author         Vladimir Popov
 * @copyright      Copyright (c) 2014 Vladimir Popov
 */

// in case redirect fails
if (Mage::registry('redirect_url')) {
    ?>
    <meta http-equiv="refresh" content="0;url=<?php echo Mage::registry('redirect_url') ?>">
    <script type="text/javascript">
        window.location = '<?php echo Mage::registry('redirect_url')?>';
    </script>
<?php
}

// get temporary data
$tmpData = Mage::getSingleton('core/session')->getData('webform_result_tmp_' . $this->getWebform()->getId());
$tmpResult = new Varien_Object();
if (!empty($tmpData['field']) && is_array($tmpData['field'])) {
    foreach ($tmpData['field'] as $field_id => $value) {
        $tmpResult->setData('field_' . $field_id, $value);
    }
}

if ($this->getWebform()->getIsActive() == VladimirPopov_WebForms_Model_Webforms::STATUS_DISABLED || !$this->isDirectAvailable()) {
    echo $this->getNotAvailableMessage();
} else {
    ?>
    <div class="block">
        <h1><?php echo $this->getWebform()->getName(); ?></h1>
    </div>

    <?php if ($this->isAjax()) { ?>
        <div id="webform_<?php echo $this->getWebform()->getId() ?>_success_text" class="std webforms-success-text"
             style="display:none"></div>
    <?php } ?>

    <?php
    if (Mage::registry('show_success')) {
        ?>
        <div class="std webforms-success-text">
            <?php echo $this->getWebform()->getSuccessText() ?>
        </div>
    <?php
    } else {
        ?>

        <style>
            .webforms-fields-comment {
                clear: both;
                padding-top: 5px
            }

            .grid {
                width: 960px !important;
            }

            .buttons-set {
                margin-bottom: 30px;
                text-align: right
            }

            .required {
                text-align: right;
                margin-bottom: 10px;
            }
        </style>

        <div class="grid">

            <div id="webform_<?php echo $this->getWebform()->getId() ?>_form" class="grid_7 suffix_1">

                <?php if ($this->isAjax()) { ?>
                    <iframe id="webform_<?php echo $this->getWebform()->getId() ?>_iframe"
                            name="webform_<?php echo $this->getWebform()->getId() ?>_iframe"
                            style="width:0;height:0;border:0;position:absolute"></iframe>
                <?php } ?>

                <form class="scaffold" action="<?php echo $this->getFormAction() ?>" method="post"
                      name="webform_<?php echo $this->getWebform()->getId() ?>"
                      id="webform_<?php echo $this->getWebform()->getId() ?>"
                      enctype="<?php echo $this->getEnctype() ?>"
                      class="webforms-<?php echo $this->getWebform()->getCode() ?>"
                      <?php if ($this->isAjax()) { ?>target="webform_<?php echo $this->getWebform()->getId() ?>_iframe"<?php } ?>>


                    <input type="hidden" name="submitWebform_<?php echo $this->getWebform()->getId() ?>" value="1"/>
                    <input type="hidden" name="webform_id" value="<?php echo $this->getWebform()->getId() ?>"/>

                    <?php if (is_array($this->getData('hidden'))) {
                        foreach ($this->getData('hidden') as $key => $value) {
                            ?>
                            <input type="hidden" name="<?php echo $key ?>" value="<?php echo $value ?>"/>
                        <?php
                        }
                    }?>

                    <?php if (count($this->getWebform()->_getHidden())) {
                        foreach ($this->getWebform()->_getHidden() as $field) {
                            echo $field->toHtml();
                        }
                    } ?>

                    <?php
                    foreach ($this->getWebform()->_getFieldsToFieldsets() as $fieldset_id => $fieldset) {
                        ?>
                        <div id="fieldset_<?php echo $fieldset_id ?>" class="fieldset-<?php echo $fieldset_id ?>"
                            <?php if ($fieldset['logic_visibility'] == 'hidden') echo 'style="display:none"'; ?>>

                            <?php if (!empty($fieldset['name'])) { ?>
                                <h2 class="legend"><?php echo $fieldset['name'] ?></h2>
                            <?php } ?>

                            <ul class="struts">
                                <?php
                                $k = 0;
                                $j = 0;
                                $prev = false;
                                $fields_count = count($fieldset['fields']);
                                foreach ($fieldset['fields'] as $field) {

                                    // set temporary value
                                    $field->setData('result', $tmpResult);

                                    $size_class = "fields";
                                    $field_class = "field";
                                    if ($field->getSize() == 'wide') {
                                        $size_class = "wide";
                                        $field_class = "";
                                    }

                                    ?>
                                    <li <?php if ($size_class == 'wide') { ?>id="field_<?php echo $field->getId() ?>_row" <?php } ?>
                                        class="<?php if ($size_class == 'wide') { ?>webforms-fields-row-<?php echo $field->getId() ?><?php } ?>"
                                        <?php if ($size_class == 'wide' && $field->getData('logic_visibility') == 'hidden') echo 'style="display:none"'; ?>>
                                        <div id="field_<?php echo $field->getId() ?>"
                                             class="<?php echo $field_class ?> webforms-fields-<?php echo $field->getId() ?><?php if ($field->getCode()) { ?> webforms-fields-<?php echo $field->getCode();
                                             }?>"
                                            <?php if ($field->getData('logic_visibility') == 'hidden') echo 'style="display:none"'; ?>>

                                            <?php if ($field->getType() != 'html' && $field->getType() != 'subscribe') { ?>
                                                <label for="field_<?php echo $field->getId() ?>">
                                                    <?php echo $field->getName() ?>
                                                    <?php if ($field->getRequired()) { ?>
                                                        <abbr title="Required">*</abbr>
                                                    <?php } ?>

                                                </label>
                                            <?php } ?>

                                            <div class="input-box">
                                                <?php echo $field->toHtml(); ?>
                                            </div>

                                        </div>
                                    </li>
                                    <?php
                                    $k++;
                                    $j++;
                                    $prev = $field;
                                }?>
                            </ul>
                        </div>

                    <?php
                    } // foreach 'fields_to_fieldsets'
                    ?>

                    <?php if (Mage::registry('use_captcha')) { ?>
                        <div class="fieldset">
                            <h2 class="legend">Captcha</h2>
                            <ul class="form-list">
                                <li class="fields">
                                    <div class="field">
                                        <?php echo $this->getCaptcha()->getHTML() ?>
                                    </div>
                                </li>
                            </ul>
                        </div>
                    <?php } ?>

                    <?php if (Mage::getStoreConfig('webforms/honeypot/enable')) {
                        echo $this->getLayout()->createBlock(
                            'core/template',
                            'webform_' . $this->getWebform()->getId() . '_honeypot',
                            array(
                                'webform' => $this->getWebform(),
                                'template' => 'webforms/scripts/honeypot.phtml',
                            )
                        )->toHtml();
                    }?>

                    <div class="buttons-set">
                        <p class="required"><?php echo $this->__('* Required Fields') ?></p>
                        <button type="button" class="button"
                                id="webform_<?php echo $this->getWebform()->getId() ?>_submit_button"
                                onclick="webform_<?php echo $this->getWebform()->getId() ?>_submit()"
                                title="<?php echo $this->__('submit') ?>">
			<span>
				<span><?php echo $this->__($this->getWebform()->getSubmitButtonText()) ?></span>
			</span>
                        </button>
		<span class="please-wait" id="webform_<?php echo $this->getWebform()->getId() ?>_sending_data"
              style="display:none;">
			<span
                id="webform_<?php echo $this->getWebform()->getId() ?>_progress_text"><?php echo $this->__('Sending...') ?></span>
		</span>
                    </div>

                </form>

                <?php if ($this->getWebform()->getDescription()) { ?>
                    <div class="grid_4">
                        <?php echo $this->getWebform()->getDescription(); ?>
                    </div>
                <?php } ?>

            </div>

        </div><!-- grid-->

        <?php echo $this->getLayout()->createBlock(
            'core/template',
            'webform_' . $this->getWebform()->getId() . '_submit_script',
            array(
                'webform' => $this->getWebform(),
                'ajax' => $this->isAjax(),
                'template' => 'webforms/scripts/submit.phtml',
            )
        )->toHtml();
        ?>

    <?php
    } // if(show_success)
} // is active
?>